package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.RequiredPermission;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	private IDepartmentService service;
	
	@RequestMapping("/list")
	@RequiresPermissions("department:list")
	@RequiredPermission("部门列表")
	public String list(Model model,QueryObject qo){
		model.addAttribute("result", service.query(qo));
		return "department/list";
	}
	@RequestMapping("/delete")
	@RequiredPermission("部门删除")
	@RequiresPermissions("department:delete")
	@ResponseBody
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}
	
	@RequiredPermission("部门编辑")
	@RequestMapping("/input")
	@RequiresPermissions("department:input")
	public String input(Long id,Model model){
		if (id!=null) {
			model.addAttribute("dept", service.get(id));
		}
		return "department/input";
	}
	@RequiredPermission("部门保存或更新")
	@RequiresPermissions("department:saveOrUpdate")
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(Department department){
		if (department.getId()!=null) {
			service.update(department);
		}else{
			service.save(department);
		}
		return "redirect:/department/list";
	}
	
}
